/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CekPassword.java
 *
 * Created on Nov 9, 2011, 2:02:34 PM
 */
package Manager.Login;

import Manager.TestConnection;
import Utama.Utama;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Piyo
 */
public class CekPassword extends javax.swing.JDialog {

    /** Creates new form CekPassword */
    public CekPassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        passwordLabel = new javax.swing.JLabel();
        SendButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        TextPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        passwordLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        passwordLabel.setForeground(new java.awt.Color(0, 0, 153));
        passwordLabel.setText("Password");

        SendButton.setText("Send");
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        TextPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TextPasswordKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TextPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(SendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(TextPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SendButton)
                    .addComponent(CancelButton))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-305)/2, (screenSize.height-153)/2, 305, 153);
    }// </editor-fold>//GEN-END:initComponents

private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
    if (TextPassword.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "please entry password");
    } else {
        try {
            String password = TextPassword.getText();
            if (CekPassword.validasi(password)) {
                JOptionPane.showMessageDialog(this, "Welcome");
                ((Utama) this.getParent()).setSt(true);
                this.dispose();
            } else {
                CekPassword.pesan_Error();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

}//GEN-LAST:event_SendButtonActionPerformed

private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
    ((Utama) this.getParent()).setSt(false);
    this.dispose();
}//GEN-LAST:event_CancelButtonActionPerformed

private void TextPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TextPasswordKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        SendButton.doClick();
    }
}//GEN-LAST:event_TextPasswordKeyPressed

    public static void pesan_Error() {
        JOptionPane.showMessageDialog(null, "invalid username or password");
    }

    public static boolean validasi(String password) throws SQLException {
        TestConnection conn = new TestConnection();
        Connection test = conn.getConnection();
        String query = "select * from OWNER where password = ?";
        PreparedStatement ps = test.prepareStatement(query);
        ps.setString(1, password);
        return ps.executeQuery().next();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton SendButton;
    private javax.swing.JPasswordField TextPassword;
    private javax.swing.JLabel passwordLabel;
    // End of variables declaration//GEN-END:variables
}
